/**
 * @class Ext.app.LoaiHangHoaPanel
 * @extends Ext.panel.Panel
 */
Ext.define('Ext.app.LoaiHangHoaPanel', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.loaiHangHoaPanel',
    autoScroll: true,
    border: true,
    layout: 'border',
    titleAlign: 'center',
    items: [
        {
            region: 'center',
            xtype: 'cell-editing',
            columnLines: true,
            store: {
                fields: ['pk', 'name'],
                pageSize: 25,
                proxy: {
                    type: 'ajax',
                    url: 'LoaiHangHoa.jsp',
                    reader: {
                        type: 'json',
                        root: 'root'
                    }
                }
            },
            columns: [{
                text: this.translations.get("HOME.LOAI_HANG_HOA"),
                draggable: false,
                menuDisabled: true,
                dataIndex: 'name',
                editor: {
                    allowBlank: false
                },
                flex: 1
            }],
            viewConfig: {
                loadingText: this.translations.get("HOME.PLEASE_WAIT"),
                markDirty: false,
                stripeRows: true
            },
            tbar: [{
                icon: 'images/add.png',
                handler: function(button) {
                    var grid = button.up('cell-editing');
                    grid.addRow({}, 0);
                    grid.down('tbseparator').show();
                    grid.down('[name=saveButton]').show();
                }
            },
            {
                xtype: 'tbseparator',
                hidden: true
            },
            {
                name: 'saveButton',
                icon: 'images/disk_blue.png',
                hidden: true,
                handler: function(button) {
                    var grid = button.up('cell-editing');
                    grid.cellEditing.completeEdit();
                    var newRecords = grid.getStore().getNewRecords();
                    var iLength = newRecords.length;
                    var records = [];
                    for(var i=0;i<iLength; i++) {
                        var r = newRecords[i];
                        records.push({
                            name: r.get("name")
                        });
                    }
                    var param = {
                        records: Ext.JSON.encode(records)
                    };
                    Ext.getBody().mask(translations.get("HOME.PLEASE_WAIT"));
                    Ext.Ajax.request({
                        url: 'LuuLoaiHangHoa.jsp',
                        params: param,
                        method: 'POST',
                        success: function(response, opts) {
                            Ext.getBody().unmask();
                            var obj = Ext.decode(response.responseText);
                            if(!obj.success) {
                                showErrorMsg(obj.msg);
                            }
                            else {
                                displayPanel({
                                    xtype: 'loaiHangHoaPanel',
                                    title: translations.get("HOME.LOAI_HANG_HOA_TITLE")
                                });
                            }
                        },
                        failure: function(response, opts) {
                            Ext.getBody().unmask();
                            alert('server-side failure with status code ' + response.status);
                        }
                    });
                }
            }],
            afterFirstLayout: function() {
                this.on("beforeedit", function(editor, e, eOpts) {
                    var pk = e.record.get("pk");
                    return (pk == null || pk == "");
                });
                this.getStore().load();
            }
        },
    ]
});
